package com.xuanxuan.socketdemo.activity;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;

import com.xuanxuan.socketdemo.R;
import com.ywicc.commlibs.socket.ClientManger;
import com.ywicc.commlibs.socket.SocketUtils;

import java.util.Arrays;

public class ClientActivity extends AppCompatActivity implements View.OnClickListener, ClientManger.ClientMangerImpl {

    private ClientManger clientManger;

    private final String TAG = "ClientActivity";

    // 指定对应的 IP 地址和端口
    private String strIP = "10.14.14.18";
    private int port = 8090;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.client_activity);

        Button btn_start = findViewById(R.id.btn_client_start);
        Button btn_stop = findViewById(R.id.btn_client_stop);
        Button btn_bind = findViewById(R.id.btn_client_bind);
        Button btn_unbind = findViewById(R.id.btn_client_unbind);
        Button btn_receive = findViewById(R.id.btn_client_receive);

        RecyclerView recyclerView = findViewById(R.id.rv_client);

        btn_start.setOnClickListener(this);
        btn_stop.setOnClickListener(this);
        btn_bind.setOnClickListener(this);
        btn_unbind.setOnClickListener(this);
        btn_receive.setOnClickListener(this);

        clientManger = new ClientManger();

        // 与小车通讯
//        WifiUtils wifiUtils = new WifiUtils(ClientActivity.this);
//        strIP = wifiUtils.getServerIP();
//        port = 60000;
    }

    @SuppressLint("NonConstantResourceId")
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.btn_client_start:
                clientManger.initSocket(strIP, port);
                break;
            case R.id.btn_client_stop:
                clientManger.stopSocket();
                break;
            case R.id.btn_client_bind:
                clientManger.setStrictModule(SocketUtils.SERVER_RECEIVE_STRICT);
                byte[] data = new byte[]{0x55, (byte) 0xAA, 0x01, 0x01, 0x00, 0x00, 0x00, (byte) 0xbb};
                clientManger.sendData(data);
                break;
            case R.id.btn_client_unbind:
                clientManger.receiveData();
                break;
            case R.id.btn_client_receive:
//                clientManger.setStrictModule(SocketUtils.SERVER_RECEIVE_STRICT);
                clientManger.startLongReceiveData();
                break;
            case R.id.btn_client_stop_receive:
                clientManger.stopLongReceiveData();
                break;
        }
    }

    @Override
    public void callBackClientReceiveData(byte[] data) {
        // 输出接收到的数据
        if (data.length > 0) {
            Log.w(TAG, "接收到的数据为: " + Arrays.toString(data));
        }
    }
}